<template>
  <div class="department-management">
    <el-card>
      <div class="header">
        <h2>部门管理</h2>
        <el-button type="primary" @click="handleAdd">新增部门</el-button>
      </div>
      
      <el-table :data="departmentList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="部门名称" />
        <el-table-column prop="description" label="部门描述" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="部门描述" prop="description">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const departmentList = ref([
  { id: 1, name: '技术部', description: '负责技术开发工作' },
  { id: 2, name: '市场部', description: '负责市场推广工作' },
  { id: 3, name: '人事部', description: '负责人事管理工作' }
])

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const isEdit = ref(false)
const currentId = ref(0)

const form = reactive({
  name: '',
  description: ''
})

const rules = {
  name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
  description: [{ required: true, message: '请输入部门描述', trigger: 'blur' }]
}

const handleAdd = () => {
  dialogTitle.value = '新增部门'
  isEdit.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑部门'
  isEdit.value = true
  currentId.value = row.id
  form.name = row.name
  form.description = row.description
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该部门吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    departmentList.value = departmentList.value.filter(dep => dep.id !== row.id)
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const submitForm = () => {
  formRef.value.validate(valid => {
    if (valid) {
      if (isEdit.value) {
        const index = departmentList.value.findIndex(dep => dep.id === currentId.value)
        if (index !== -1) {
          departmentList.value[index] = {
            id: currentId.value,
            name: form.name,
            description: form.description
          }
        }
        ElMessage.success('修改成功')
      } else {
        const newId = departmentList.value.length > 0 
          ? Math.max(...departmentList.value.map(dep => dep.id)) + 1 
          : 1
        
        departmentList.value.push({
          id: newId,
          name: form.name,
          description: form.description
        })
        ElMessage.success('添加成功')
      }
      
      dialogVisible.value = false
    }
  })
}

const resetForm = () => {
  form.name = ''
  form.description = ''
}
</script>

<style scoped>
.department-management {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>